Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Multithreading → Creating Thread

Multithreading

Creating Thread

Creating a Thread

There are two primary ways to create threads in Java: 1. Extending the Thread Class This approach involves creating a subclass of the java.lang.Thread class. Here's a breakdown of the steps: ⯄ Define a Subclass: Create a new class that inherits from java.lang.Thread. ⯄ Override the run() Method: This method is the heart of your thread. It defines the code that the thread will execute when it's started. The run() method should contain your application logic for the separate thread. ⯄ Create an Instance: Instantiate your newly created subclass to create a thread object. ⯄ Start the Thread: Call the start() method on the thread object. This method initiates the actual execution of the thread's run() method in a separate thread of control. Here's an example:
Creating thread by extending thread class public class Main extends Thread { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " - Iteration: " + i); } } public static void main(String[] args) { Main thread = new Main(); thread.start(); // Main thread code } }

Output

Thread-0 - Iteration: 0 Thread-0 - Iteration: 1 Thread-0 - Iteration: 2 Thread-0 - Iteration: 3 Thread-0 - Iteration: 4

2. Implementing the Runnable Interface This approach offers more flexibility in separating the thread's logic from the thread management aspects. Here's the process: ⯄ Implement the Runnable Interface: Create a class that implements the Runnable interface. This interface has a single abstract method, run(), which defines the thread's execution logic. ⯄ Create a Thread Object: Instantiate the Thread class, passing your Runnable class instance to its constructor. This associates your code with the thread object. ⯄ Start the Thread: Call the start() method on the Thread object, similar to the first approach. Example:
Creating thread by Runnable Interface public class Main implements Runnable { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " - Iteration: " + i); } } public static void main(String[] args) { Main runnable = new Main(); Thread thread = new Thread(runnable); thread.start(); // Main thread code } }

Output

Thread-0 - Iteration: 0 Thread-0 - Iteration: 1 Thread-0 - Iteration: 2 Thread-0 - Iteration: 3 Thread-0 - Iteration: 4

Thread Class vs Runnable Interface

⯄ If we extend the Thread class, our class cannot extend any other class because Java doesn't support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes. ⯄ We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface. ⯄ Using runnable will give you an object that can be shared amongst multiple threads.

Tutorials